package com.ms.teapuesto.security;

import com.ms.jsf.ViewConstant;
import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import java.util.Iterator;

/**
 * User: AW
 * Date: 24/02/13
 */
public class ValidationErrorPhaseListener implements PhaseListener {
    public PhaseId getPhaseId() {
//        return PhaseId.PROCESS_VALIDATIONS;
        return PhaseId.ANY_PHASE;
    }

    public void afterPhase(final PhaseEvent event) {
//        System.out.println("AFTER XXX" + event.getPhaseId());
        final Iterable<ExceptionQueuedEvent> exceptionEvents = event.getFacesContext().getExceptionHandler().getUnhandledExceptionQueuedEvents();
        for (final ExceptionQueuedEvent exceptionEvent : exceptionEvents) {
            // This print out goes (found in server.log).
            System.out.println("Problem: " + exceptionEvent.getContext().getException());
            // But this message does not.
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Problem", "Problem: " + exceptionEvent.getContext().getException()));
        }
        Iterator<FacesMessage> messages = event.getFacesContext().getMessages();
        while (messages.hasNext()) {
            FacesMessage message = messages.next();
//            System.out.println("Message:" + message);
            showOrHideModal(ViewConstant.WV_CUSTOM_MESSAGE, true);
            messages.remove();
        }
    }

    public void beforePhase(PhaseEvent event) {
//        System.out.println("BEFORE XXX" + event.getPhaseId());
        final Iterable<ExceptionQueuedEvent> exceptionEvents = event.getFacesContext().getExceptionHandler().getUnhandledExceptionQueuedEvents();
        for (final ExceptionQueuedEvent exceptionEvent : exceptionEvents) {
            // This print out goes (found in server.log).
            System.out.println("Problem: " + exceptionEvent.getContext().getException());
            // But this message does not.
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Problem", "Problem: " + exceptionEvent.getContext().getException()));
        }

    }


    protected void showOrHideModal(String dialogWv, boolean show) {
        RequestContext context = RequestContext.getCurrentInstance();
        String command = dialogWv + ".hide()";
        if (!show) {
            context.execute(command);
            return;
        }
        command = dialogWv + ".show()";
        context.execute(command);
        String dlgPrefix = ViewConstant.PREFIX_DIALOG_ID;
        String zoneToUpdate = dlgPrefix + dialogWv.substring(2);
//        List<String> toUpdate = new ArrayList<String>();
        if (dialogWv.equals(ViewConstant.WV_CUSTOM_MESSAGE)) {
//            toUpdate.add(ViewConstant.PNL_CUSTOM_MESSAGE);
//            context.update(toUpdate);
            context.update(ViewConstant.PNL_CUSTOM_MESSAGE);
            return;
        }
//        toUpdate.add(zoneToUpdate);
//        context.update(toUpdate);
        context.update(zoneToUpdate);
    }

}
